#include <lkhos/tty/tty.h>

uint16_t* buf_start = 0xB8000;
uint16_t theme_color = VGA_COLOR_BLACK;
uint32_t TTY_COLUMN = 0;
uint32_t TTY_ROW = 0;

void tty_set_theme(uint16_t fg, uint16_t bg) {
    /* L_BG 1b | bg 3b | fg 4b | ascii 8 bit */
    theme_color = ((bg << 4) | fg) << 8;
}

void tty_put_char(char chr) {
    *(buf_start + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr);
    TTY_COLUMN++;
    if (TTY_COLUMN >= TTY_WIDTH)
    {
        TTY_COLUMN = 0;
        TTY_ROW++;
        if (TTY_ROW >= TTY_HEIGHT)
        {
            tty_scroll_up();
            TTY_ROW--;
        }
        
    }
    
}

void tty_put_str(char* str) {
    while (*str != 0)
    {
        tty_put_char(*str);
        str++;
    }
    
}

void tty_scroll_up() {
    // TODO
}

void tty_clear() {
    for(int i = 0; i < TTY_WIDTH; i++) {
        for(int j = 0; j < TTY_HEIGHT; j++) {
            *(buf_start + i + j * TTY_WIDTH) = theme_color;
        }
    }
    TTY_ROW = TTY_COLUMN = 0;
}